﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web
{
    public partial class UpdateCouArrange : Base.BasePage
    {
        public int IDUniq = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("SYSKCTZ");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                YearDDL_DataBind();//学年
                CourseDDL_DataBind();//课程
                WeekDDL_DataBind();//周次
                if (CourseDDL.SelectedValue == "")
                {
                    return;
                }
                ArrangementGridView_DataBind(int.Parse(CourseDDL.SelectedValue), "0", "0", "", "");//GridView
            }
            CourseArrangeList();
        }

        //绑定GridView
        protected void ArrangementGridView_DataBind(int CourseID, string Week, string WeekDay, string LocationName, string TeacherName)
        {
            List<LabMS.Model.Arrangement> arrModelList = new List<LabMS.Model.Arrangement>();
            ExtendBLL.Arrangement couArrange = new ExtendBLL.Arrangement();
            DataSet ds = new DataSet();
            ds = couArrange.GetAllArrangement(CourseID, Week, WeekDay, LocationName, TeacherName);
            arrModelList = HandleList(ds);

            ArrangementGridView.DataSource = arrModelList;
            ArrangementGridView.DataBind();
        }

        //处理DS列表
        protected List<LabMS.Model.Arrangement> HandleList(DataSet ds)
        {
            List<LabMS.Model.Arrangement> arrList = new List<LabMS.Model.Arrangement>();
            LabMS.BLL.Arrangement arr = new LabMS.BLL.Arrangement();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                List<LabMS.Model.Arrangement> arrModelList = new List<LabMS.Model.Arrangement>();
                string strClassName = "";
                List<int> ClassID = new List<int>();
                string strTeaName = "";
                List<int> TeacherID = new List<int>();
                arrModelList = arr.GetModelList("LocationID=" + int.Parse(ds.Tables[0].Rows[i]["LocationID"].ToString()) + " and [WeekDay]=" + int.Parse(ds.Tables[0].Rows[i]["WeekDay"].ToString()) + " and Lession=" + int.Parse(ds.Tables[0].Rows[i]["Lession"].ToString()) + " and CourseID=" + int.Parse(ds.Tables[0].Rows[i]["CourseID"].ToString()) + " and [Week]='" + ds.Tables[0].Rows[i]["Week"].ToString() + "' and ClassID is not null and TeacherID is not null and CourseID is not null and [Week] is not null");
                for (int j = 0; j < arrModelList.Count; j++)
                {
                    bool IsClassID = true;
                    for (int m = 0; m < ClassID.Count; m++)
                    {
                        if (int.Parse(arrModelList[j].ClassID.ToString()) == ClassID[m])
                        {
                            IsClassID = false;
                        }
                    }
                    if (IsClassID)
                    {
                        ClassID.Add(int.Parse(arrModelList[j].ClassID.ToString()));
                        strClassName += arrModelList[j].ClassName + " ";//班级名称
                    }
                    bool IsTeacherID = true;
                    for (int m = 0; m < TeacherID.Count; m++)
                    {
                        if (int.Parse(arrModelList[j].TeacherID.ToString()) == TeacherID[m])
                        {
                            IsTeacherID = false;
                        }
                    }
                    if (IsTeacherID)
                    {
                        TeacherID.Add(int.Parse(arrModelList[j].TeacherID.ToString()));
                        strTeaName += arrModelList[j].TeacherName + " ";//教师名称
                    }
                }
                strClassName = strClassName.Trim();
                strTeaName = strTeaName.Trim();

                LabMS.Model.Arrangement arrModel = new LabMS.Model.Arrangement();
                arrModel.LabName = ds.Tables[0].Rows[i]["ID"].ToString();//暂把实验室名称存储各ID
                arrModel.CourseID = int.Parse(ds.Tables[0].Rows[i]["CourseID"].ToString());
                arrModel.CourseName = ds.Tables[0].Rows[i]["CourseName"].ToString();
                arrModel.ClassName = strClassName;
                arrModel.TeacherName = strTeaName;
                arrModel.Week = ds.Tables[0].Rows[i]["Week"].ToString();
                arrModel.WeekDay = int.Parse(ds.Tables[0].Rows[i]["WeekDay"].ToString());
                arrModel.LocationName = ds.Tables[0].Rows[i]["LocationName"].ToString();
                arrModel.Lession = int.Parse(ds.Tables[0].Rows[i]["Lession"].ToString());
                arrList.Add(arrModel);
            }
            return arrList;
        }

        //绑定课程
        protected void CourseDDL_DataBind()
        {
            string strYear = YearDDL.SelectedValue;
            string strTerm = TermDDL.SelectedValue;

            LabMS.BLL.Course cou = new LabMS.BLL.Course();
            List<LabMS.Model.Course> couModelList = new List<LabMS.Model.Course>();

            couModelList = cou.GetModelList("CourseYear=" + strYear + " and CourseSeason=" + strTerm);
            CourseDDL.DataSource = couModelList;
            CourseDDL.DataBind();
        }

        //绑定学年
        protected void YearDDL_DataBind()
        {
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            listCClass = eDictionary.GetModelList("DClass = 'ruxuenianfen' order by Code desc");
            YearDDL.DataSource = listCClass;
            YearDDL.DataTextField = "Name";
            YearDDL.DataValueField = "Code";
            YearDDL.DataBind();
        }

        //学年 学期 索引改变
        protected void DropDownList_OnSelectIndexChange(object sender, EventArgs e)
        {
            string strYear = YearDDL.SelectedValue;
            string strTerm = TermDDL.SelectedValue;

            LabMS.BLL.Course cou = new LabMS.BLL.Course();
            List<LabMS.Model.Course> couModelList = new List<LabMS.Model.Course>();

            couModelList = cou.GetModelList("CourseYear=" + strYear + " and CourseSeason=" + strTerm);
            CourseDDL.DataSource = couModelList;
            CourseDDL.DataBind();
            if (CourseDDL.SelectedValue == "")
            {
                ArrangementGridView_DataBind(0, WeekDDL.SelectedValue, WeekDayDDL.SelectedValue, LocationBTX.Text.Trim(), TeacherTBX.Text.Trim());
            }
            else
            {
                ArrangementGridView_DataBind(int.Parse(CourseDDL.SelectedValue), WeekDDL.SelectedValue, WeekDayDDL.SelectedValue, LocationBTX.Text.Trim(), TeacherTBX.Text.Trim());
            }
        }

        //课程索引改变
        protected void CourseDDL_OnSelectIndexChange(object sender, EventArgs e)
        {
            ArrangementGridView_DataBind(int.Parse(CourseDDL.SelectedValue), WeekDDL.SelectedValue, WeekDayDDL.SelectedValue, LocationBTX.Text.Trim(), TeacherTBX.Text.Trim());
        }

        //调整
        protected void ReSet_OnCommand(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "ReSet")
            {
                LabMS.BLL.Arrangement arr = new LabMS.BLL.Arrangement();
                List<LabMS.Model.Arrangement> arrModelList = new List<LabMS.Model.Arrangement>();
                string[] strArray = e.CommandArgument.ToString().Split(',');
                string Week;
                int WeekDay, Lession, LocationID;

                IDHF.Value = e.CommandArgument.ToString();
                Week = strArray[0];
                WeekDay = int.Parse(strArray[1]);
                Lession = int.Parse(strArray[2]);
                LocationID = int.Parse(strArray[3]);
                arrModelList = arr.GetModelList("[Week]='" + Week + "' and [WeekDay]=" + WeekDay + " and Lession=" + Lession + " and LocationID=" + LocationID);
                LabDropdowlist();//绑定实验室
                LabDDL.SelectedValue = arrModelList[0].LabID.ToString();
                HouseDropDownList();//绑定用房
                HouseDDL.SelectedValue = arrModelList[0].LocationID.ToString();
                WeekDropDownList();//绑定周次
                WeekSelectDDL.SelectedValue = arrModelList[0].Week;
                #region 绑定教课教师
                DataSet teacherList = new DataSet();
                teacherList = GetTeacherList(Week, WeekDay, Lession, LocationID);

                string strHTML = "<table id='studenlist' class='infoTable' border='1' style='border-collapse: collapse;'>";
                strHTML += "<thead>";
                strHTML += "<tr class='header'>";
                strHTML += "<th style='display:none'>";
                strHTML += "ID";
                strHTML += "</th>";

                strHTML += "<th>";
                strHTML += "教师编号";
                strHTML += "</th>";

                strHTML += "<th>";
                strHTML += "教师姓名";
                strHTML += "</th>";
                strHTML += "<th>";
                strHTML += "";
                strHTML += "</th>";
                strHTML += "</tr>";
                strHTML += "</thead>";
                strHTML += "<tbody>";
                tb_StudentIDs.Text = teacherList.Tables[0].Rows[0]["ID"].ToString();
                tb_StudentNames.Value = teacherList.Tables[0].Rows[0]["Teacher_Name"].ToString();
                strHTML += "<tr>";
                strHTML += "<td style='display:none'>";
                strHTML += teacherList.Tables[0].Rows[0]["ID"].ToString();
                strHTML += "</td>";
                strHTML += "<td>";
                strHTML += teacherList.Tables[0].Rows[0]["Teacher_Code"].ToString();
                strHTML += "</td>";
                strHTML += "<td>";
                strHTML += teacherList.Tables[0].Rows[0]["Teacher_Name"].ToString();
                strHTML += "</td>";
                strHTML += "<td>";
                strHTML += "<a>删除</a>";
                strHTML += "</td>";
                strHTML += "</tr>";
                strHTML += "</tbody>";
                strHTML += "</table>";
                tblistStudents.Value = strHTML;
                tblistEmpty.Value = "0";
                #endregion
                #region 绑定上课班级
                DataSet classList = new DataSet();
                classList = GetClassList(Week, WeekDay, Lession, LocationID);

                string strHTML2 = "<table  id='studenlist' class='infoTable' border='1' style='border-collapse: collapse;'>";
                strHTML2 += "<thead>";
                strHTML2 += "<tr class='header'>";
                strHTML2 += "<th style='display:none'>";
                strHTML2 += "ID";
                strHTML2 += "</th>";
                strHTML2 += "<th>";
                strHTML2 += "班级编号";
                strHTML2 += "</th>";
                strHTML2 += "<th>";
                strHTML2 += "班级名称";
                strHTML2 += "</th>";
                strHTML2 += "<th>";
                strHTML2 += "专业";
                strHTML2 += "</th>";
                strHTML2 += "<th>";
                strHTML2 += "班级人数";
                strHTML2 += "</th>";
                strHTML2 += "<th>";
                strHTML2 += "删除";
                strHTML2 += "</th>";
                strHTML2 += "</tr>";
                strHTML2 += "</thead>";
                strHTML2 += "<tbody>";
                tb_StudentIDs2.Value = "";
                for (int i = 0; i < classList.Tables[0].Rows.Count; i++)
                {
                    if (tb_StudentIDs2.Value == "")
                    {
                        tb_StudentIDs2.Value += classList.Tables[0].Rows[i]["ID"].ToString();
                        tb_StudentNames2.Value += classList.Tables[0].Rows[i]["Class_Name"].ToString();
                    }
                    else
                    {
                        tb_StudentIDs2.Value += "," + classList.Tables[0].Rows[i]["ID"].ToString();
                        tb_StudentNames2.Value += "," + classList.Tables[0].Rows[i]["Class_Name"].ToString();
                    }
                    strHTML2 += "<tr>";
                    strHTML2 += "<td style='display:none'>";
                    strHTML2 += classList.Tables[0].Rows[i]["ID"].ToString();
                    strHTML2 += "</td>";
                    strHTML2 += "<td>";
                    strHTML2 += classList.Tables[0].Rows[i]["Class_Code"].ToString();
                    strHTML2 += "</td>";
                    strHTML2 += "<td>";
                    strHTML2 += classList.Tables[0].Rows[i]["Class_Name"].ToString();
                    strHTML2 += "</td>";
                    strHTML2 += "<td>";
                    strHTML2 += classList.Tables[0].Rows[i]["Class_Professional"].ToString();
                    strHTML2 += "</td>";
                    strHTML2 += "<td>";
                    strHTML2 += classList.Tables[0].Rows[i]["Class_StudentNum"].ToString();
                    strHTML2 += "</td>";
                    strHTML2 += "<td>";
                    strHTML2 += "<a>删除</a>";
                    strHTML2 += "</td>";
                    strHTML2 += "</tr>";
                }
                strHTML2 += "</tbody>";
                strHTML2 += "</table>";
                tblistEmpty2.Value = "0";
                tblistStudents2.Value = strHTML2;
                #endregion
                IDUniq = GetTeaMissID(int.Parse(arrModelList[0].CourseID.ToString()), YearDDL.SelectedValue, TermDDL.SelectedValue);
                tboldcourse.Text = "chkDay" + WeekDay + "Class" + Lession;
                arrtable.Visible = true;
                YearDDL.Enabled = false;
                TermDDL.Enabled = false;
                CourseDDL.Enabled = false;
                DeleteBtn.Enabled = false;
                ArrangementGridView.Enabled = false;
            }

        }

        //绑定所属实验室
        protected void LabDropdowlist()
        {
            //LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            //List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();
            //List<LabMS.Model.Lab> SonLab = new List<LabMS.Model.Lab>();

            //ParLab = lab.GetModelList("Lab_ParentID is null");
            //SonLab = lab.GetModelList("Lab_ParentID is not null");
            //int count = ParLab.Count, count1 = SonLab.Count;

            //for (int i = 0; i < count; i++)
            //{
            //    int ID = ParLab[i].ID;
            //    for (int j = 0; j < count1; j++)
            //    {
            //        LabMS.Model.Lab labModel = new LabMS.Model.Lab();
            //        int ParentID = int.Parse(SonLab[j].Lab_ParentID.ToString());

            //        if (ParentID == ID)
            //        {
            //            labModel.ID = SonLab[j].ID;
            //            labModel.Lab_Name = "|-" + SonLab[j].Lab_Name;
            //            ParLab.Insert(i + 1, labModel);
            //        }
            //    }
            //}
            //LabDDL.DataSource = ParLab;
            //LabDDL.DataBind();
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> SonLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> Lab = new List<LabMS.Model.Lab>();

            Lab = lab.GetModelList("Lab_ParentID is null");
            ParLab = lab.GetModelList("Lab_ParentID is null");
            SonLab = lab.GetModelList("Lab_ParentID is not null");
            int count = ParLab.Count, count1 = SonLab.Count;

            for (int i = 0; i < count; i++)
            {
                int ID = ParLab[i].ID;
                for (int j = 0; j < count1; j++)
                {
                    LabMS.Model.Lab labModel = new LabMS.Model.Lab();
                    int ParentID = int.Parse(SonLab[j].Lab_ParentID.ToString());

                    if (ParentID == ID)
                    {
                        labModel.ID = SonLab[j].ID;
                        labModel.Lab_Name = "|-" + SonLab[j].Lab_Name;
                        Lab.Insert(i + j + 1, labModel);
                    }
                }
            }
            LabDDL.DataSource = Lab;
            LabDDL.DataBind();
        }

        //绑定用房
        protected void HouseDropDownList()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> labModelList = new List<LabMS.Model.Lab>();
            ExtendBLL.Location loc = new ExtendBLL.Location();
            if (LabDDL.SelectedValue == "")
            {
                return;
            }
            string str = "a.LabID=" + int.Parse(LabDDL.SelectedValue);

            labModelList = lab.GetModelList("Lab_ParentID=" + int.Parse(LabDDL.SelectedValue));
            for (int i = 0; i < labModelList.Count; i++)
            {
                str += " or a.LabID=" + labModelList[i].ID.ToString();
            }

            DataSet ds = loc.GetRoomAddress(str);
            HouseDDL.DataSource = ds;
            HouseDDL.DataBind();
        }

        //绑定周次
        protected void WeekDropDownList()
        {
            for (int i = 1; i < 21; i++)
            {
                ListItem listItem = new ListItem("周" + i.ToString(), i.ToString());
                WeekSelectDDL.Items.Add(listItem);
            }
        }

        //绑定周次 查询
        protected void WeekDDL_DataBind()
        {
            ListItem li = new ListItem("--全部周次--", "0");
            WeekDDL.Items.Add(li);
            for (int i = 1; i < 21; i++)
            {
                ListItem listItem = new ListItem("周" + i.ToString(), i.ToString());
                WeekDDL.Items.Add(listItem);
            }
        }

        //实验室索引改变
        protected void LabDDL_OnSelectIndexChanged(object sender, EventArgs e)
        {
            HouseDropDownList();
        }

        //获取教学任务书ID
        public int GetTeaMissID(int ID, string Year, string Term)
        {
            ExtendBLL.Arrangement arr = new ExtendBLL.Arrangement();
            DataSet ds = new DataSet();
            int teaMisID;

            ds = arr.GetTeaMisID(ID, Year, Term);
            teaMisID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
            return teaMisID;
        }

        //获取老师列表
        protected DataSet GetTeacherList(string Week, int WeekDay, int Lession, int LocationID)
        {
            ExtendBLL.Arrangement arr = new ExtendBLL.Arrangement();
            DataSet ds = new DataSet();

            ds = arr.GetTeaInfo(Week, WeekDay, Lession, LocationID);
            return ds;
        }

        //获取班级列表
        protected DataSet GetClassList(string Week, int WeekDay, int Lession, int LocationID)
        {
            ExtendBLL.Class cla = new ExtendBLL.Class();
            DataSet ds = new DataSet();

            ds = cla.GetClassInfoByArrangement(Week, WeekDay, Lession, LocationID);
            return ds;
        }

        // 分页
        protected void ArrangementGridView_OnPageChange(object sender, GridViewPageEventArgs e)
        {
            ArrangementGridView.PageIndex = e.NewPageIndex;
            ArrangementGridView_DataBind(int.Parse(CourseDDL.SelectedValue), WeekDDL.SelectedValue, WeekDayDDL.SelectedValue, LocationBTX.Text.Trim(), TeacherTBX.Text.Trim());
        }

        //删除
        protected void Delete_OnClick(object sender, EventArgs e)
        {
            ExtendBLL.Arrangement arr = new ExtendBLL.Arrangement();
            for (int i = 0; i < ArrangementGridView.Rows.Count; i++)
            {
                CheckBox CB = (CheckBox)ArrangementGridView.Rows[i].FindControl("chk");
                if (CB.Checked)
                {
                    string[] str = ArrangementGridView.DataKeys[i].Value.ToString().Split(',');
                    arr.DeleteCouArr("[Week]='" + str[0] + "' and [WeekDay]=" + int.Parse(str[1]) + " and Lession=" + int.Parse(str[2]) + " and LocationID=" + int.Parse(str[3]));
                }
            }
            ArrangementGridView_DataBind(int.Parse(CourseDDL.SelectedValue), WeekDDL.SelectedValue, WeekDayDDL.SelectedValue, LocationBTX.Text.Trim(), TeacherTBX.Text.Trim());
        }

        protected void CourseArrangeList()
        {
            lbDayAndClass.Controls.Add(new LiteralControl("<table>"));

            for (int i = 0; i < 8; ++i)
            {
                lbDayAndClass.Controls.Add(new LiteralControl("<tr>"));
                for (int j = 0; j < 6; ++j)
                {
                    if (j == 0 && i > 0)
                    {
                        lbDayAndClass.Controls.Add(new LiteralControl("<td>"));
                        lbDayAndClass.Controls.Add(new LiteralControl(GetWeekByInt(i))); ;
                        lbDayAndClass.Controls.Add(new LiteralControl("</td>"));
                    }
                    else if (j > 0 && i == 0)
                    {
                        lbDayAndClass.Controls.Add(new LiteralControl("<td>"));
                        lbDayAndClass.Controls.Add(new LiteralControl(GetChineseNubByInt(j))); ;
                        lbDayAndClass.Controls.Add(new LiteralControl("</td>"));
                    }
                    else if (j > 0 && i > 0)
                    {
                        CheckBox check = new CheckBox();
                        check.ID = "chkDay" + i.ToString() + "Class" + j.ToString();
                        lbDayAndClass.Controls.Add(new LiteralControl("<td>"));
                        lbDayAndClass.Controls.Add(check);
                        lbDayAndClass.Controls.Add(new LiteralControl("</td>"));
                    }
                    else
                    {
                        lbDayAndClass.Controls.Add(new LiteralControl("<td>"));
                        lbDayAndClass.Controls.Add(new LiteralControl("星期\\节次")); ;
                        lbDayAndClass.Controls.Add(new LiteralControl("</td>"));
                    }
                }
                lbDayAndClass.Controls.Add(new LiteralControl("</tr>"));
            }
            lbDayAndClass.Controls.Add(new LiteralControl("</table>"));
        }

        public string GetWeekByInt(int iweek)
        {
            if (iweek == 1)
            {
                return "星期一";
            }
            if (iweek == 2)
            {
                return "星期二";
            }
            if (iweek == 3)
            {
                return "星期三";
            }
            if (iweek == 4)
            {
                return "星期四";
            }
            if (iweek == 5)
            {
                return "星期五";
            }
            if (iweek == 6)
            {
                return "星期六";
            }
            if (iweek == 7)
            {
                return "星期日";
            }
            return "";
        }

        public string GetChineseNubByInt(int iNub)
        {
            if (iNub == 1)
            {
                return "一";
            }
            if (iNub == 2)
            {
                return "二";
            }
            if (iNub == 3)
            {
                return "三";
            }
            if (iNub == 4)
            {
                return "四";
            }
            if (iNub == 5)
            {
                return "五";
            }

            return "";
        }

        protected void Search_OnClick(object sender, EventArgs e)
        {
            if (CourseDDL.SelectedValue != "")
            {
                ArrangementGridView_DataBind(int.Parse(CourseDDL.SelectedValue), WeekDDL.SelectedValue, WeekDayDDL.SelectedValue, LocationBTX.Text.Trim(), TeacherTBX.Text.Trim());
            }
        }

        //保存更改
        protected void Save_OnClick(object sender, EventArgs e)
        {
            if (tb_StudentIDs.Text == "")
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('请选择教师！')</script>");
                return;
            }
            if (tb_StudentIDs2.Value == "")
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('请选择班级！')</script>");
                return;
            }

            string[] strArray = IDHF.Value.Split(',');
            ExtendBLL.Arrangement arrExBLL = new ExtendBLL.Arrangement();

            string Week = strArray[0];
            int WeekDay = int.Parse(strArray[1]);
            int Lession = int.Parse(strArray[2]);
            int LocationID = int.Parse(strArray[3]);

            arrExBLL.DeleteCouArr(" [Week]='" + Week + "' and Weekday=" + WeekDay + " and Lession=" + Lession + " and LocationID=" + LocationID + " and CourseID=" + int.Parse(CourseDDL.SelectedValue));

            LabMS.BLL.Arrangement arrBLL = new LabMS.BLL.Arrangement();
            LabMS.Model.Arrangement arr = new LabMS.Model.Arrangement();
            LabMS.BLL.Course cou = new LabMS.BLL.Course();
            LabMS.Model.Course couModel = new LabMS.Model.Course();
            couModel = cou.GetModel(int.Parse(CourseDDL.SelectedValue));

            arr.CourseID = couModel.ID;//课程ID
            arr.Year = couModel.CourseYear;//学年
            arr.ScoreTerm = couModel.CourseSeason;//学期
            arr.CourseName = couModel.Course_Name;//课程名称
            arr.LocationID = int.Parse(HouseDDL.SelectedValue);//用房ID
            arr.LocationName = HouseDDL.SelectedItem.Text;//用房位置
            arr.LabID = int.Parse(LabDDL.SelectedValue);//实验室ID
            arr.TeacherID = int.Parse(tb_StudentIDs.Text);//教师ID
            arr.TeacherName = tb_StudentNames.Value;//教师名称
            string labName = LabDDL.Text;
            int index = labName.IndexOf("|-");
            if (index == 0)
            {
                labName = labName.Substring(index, labName.Length - index - 1);
            }
            arr.LabName = labName;//实验室名称
            String[] claListsID = tb_StudentIDs2.Value.Split(',');
            String[] claListsName = tb_StudentNames2.Value.Split(',');

            arr.CourseTeacherID = GetTeaCouID(int.Parse(tb_StudentIDs.Text), int.Parse(CourseDDL.SelectedValue));
            for (int j = 0; j < claListsID.Length; j++)
            {
                arr.ClassID = int.Parse(claListsID[j]);//班级ID
                arr.ClassName = claListsName[j];//班级名称
                arr.ChoosenClassID = GetClaCouID(int.Parse(CourseDDL.SelectedValue), int.Parse(claListsID[j]), GetTeaMissID(int.Parse(CourseDDL.SelectedValue), YearDDL.SelectedValue, TermDDL.SelectedValue));//选课班级ID

                arr.Week = WeekSelectDDL.SelectedValue;//授课周次
                bool checkClassSelected = false;
                for (int n = 1; n < 8; n++)
                {
                    arr.WeekDay = n;//授课天次

                    for (int p = 1; p < 6; p++)
                    {
                        CheckBox check2 = (CheckBox)lbDayAndClass.FindControl("chkDay" + n.ToString() + "Class" + p.ToString());
                        if (check2.Checked)
                        {
                            checkClassSelected = true;
                            arr.Lession = p;//授课节次

                            string[] strCheckArrangement = new string[3];
                            strCheckArrangement = CheckArrangement(arr);
                            if (strCheckArrangement[0] != "")
                            {
                                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('课程安排冲突！" + arr.LocationName + "在第" + arr.Week.ToString() + "周 星期" + arr.WeekDay.ToString() + " 第" + arr.Lession.ToString() + "大节已安排课程：" + strCheckArrangement[0] + "')</script>");
                                return;
                            }
                            if (strCheckArrangement[1] != "")
                            {
                                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('老师课程安排冲突！" + arr.TeacherName + "在第" + arr.Week.ToString() + "周 星期" + arr.WeekDay.ToString() + " 第" + arr.Lession.ToString() + "大节已安排课程：" + strCheckArrangement[1] + "')</script>");
                                return;
                            }
                            if (strCheckArrangement[3] != "")
                            {
                                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('班级安排冲突！" + arr.ClassName + "在第" + arr.Week.ToString() + "周 星期" + arr.WeekDay.ToString() + " 第" + arr.Lession.ToString() + "大节已安排课程：" + strCheckArrangement[3] + "')</script>");
                                return;
                            }
                            if (strCheckArrangement[4] != "")
                            {
                                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('老师地点安排冲突！在第" + arr.Week.ToString() + "周 星期" + arr.WeekDay.ToString() + " 第" + arr.Lession.ToString() + "大节已安排老师：" + strCheckArrangement[4] + "上课')</script>");
                                return;
                            }
                            if (strCheckArrangement[2] != "")
                            {
                                ;
                            }
                            else
                            {
                                arrBLL.Add(arr);
                            }
                        }
                    }
                }
                if (!checkClassSelected)
                {
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('请选择上课节次！')</script>");
                    return;
                }
            }
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('修改成功！')</script>");
            arrtable.Visible = false;
            YearDDL.Enabled = true;
            TermDDL.Enabled = true;
            CourseDDL.Enabled = true;
            DeleteBtn.Enabled = true;
            ArrangementGridView.Enabled = true;
            ArrangementGridView_DataBind(int.Parse(CourseDDL.SelectedValue), WeekDDL.SelectedValue, WeekDayDDL.SelectedValue, LocationBTX.Text.Trim(), TeacherTBX.Text.Trim());
        }

        //添加并获取老师课程ID
        protected int GetTeaCouID(int TeaID, int CouID)
        {
            LabMS.BLL.TeacherCourse teaCou = new LabMS.BLL.TeacherCourse();
            LabMS.Model.TeacherCourse teaCouModel = new LabMS.Model.TeacherCourse();
            List<LabMS.Model.TeacherCourse> teaCouModelList = new List<LabMS.Model.TeacherCourse>();
            int ID;

            teaCouModelList = teaCou.GetModelList("TeacherID=" + TeaID + " and CourseID=" + CouID);
            if (teaCouModelList.Count == 0)
            {
                teaCouModel.CourseID = CouID;
                teaCouModel.TeacherID = TeaID;
                teaCou.Add(teaCouModel);
                ID = teaCou.GetMaxId() - 1;
            }
            else
            {
                ID = teaCouModelList[0].ID;
            }
            return ID;
        }

        //获取选课班级ID
        protected int? GetClaCouID(int CourseID, int ClassID, int TeaMisID)
        {
            LabMS.BLL.ClassItem claIte = new LabMS.BLL.ClassItem();
            List<LabMS.Model.ClassItem> claIteModel = new List<LabMS.Model.ClassItem>();

            claIteModel = claIte.GetModelList("ClassID=" + ClassID + " and CourseID=" + CourseID + " and TeachMissionID=" + TeaMisID);
            int? ID;
            if (claIteModel.Count == 0)
            {
                ID = null;
            }
            else
            {
                ID = int.Parse(claIteModel[0].ID.ToString());
            }
            return ID;
        }

        //检验课程是否有冲突
        protected string[] CheckArrangement(LabMS.Model.Arrangement arrModel)
        {
            LabMS.BLL.Arrangement arr = new LabMS.BLL.Arrangement();
            List<LabMS.Model.Arrangement> arrModelList = new List<LabMS.Model.Arrangement>();
            string[] checkArrangement = new string[5];
            checkArrangement[0] = "";
            checkArrangement[1] = "";
            checkArrangement[2] = "";
            checkArrangement[3] = "";
            checkArrangement[4] = "";
            arrModelList = arr.GetModelList(" LocationID=" + arrModel.LocationID + " and [Week]='" + arrModel.Week + "' and Lession=" + arrModel.Lession + " and [WeekDay]=" + arrModel.WeekDay + " and CourseID<>" + arrModel.CourseID + " and ScoreTerm='" + arrModel.ScoreTerm + "' and Year='" + arrModel.Year + "'");//课程冲突
            if (arrModelList.Count > 0)
            {
                checkArrangement[0] = arrModelList[0].CourseName;
            }

            List<LabMS.Model.Arrangement> arrModelList2 = new List<LabMS.Model.Arrangement>();
            arrModelList2 = arr.GetModelList(" [Week]='" + arrModel.Week + "' and Lession=" + arrModel.Lession + " and [WeekDay]=" + arrModel.WeekDay + " and CourseID<>" + arrModel.CourseID + " and TeacherID=" + arrModel.TeacherID + " and ScoreTerm='" + arrModel.ScoreTerm + "' and Year='" + arrModel.Year + "'");//老师课程安排冲突(同时上2门课程)
            if (arrModelList2.Count > 0)
            {
                checkArrangement[1] = arrModelList2[0].CourseName;
            }

            List<LabMS.Model.Arrangement> arrModelList4 = new List<LabMS.Model.Arrangement>();
            arrModelList4 = arr.GetModelList(" [Week]='" + arrModel.Week + "' and Lession=" + arrModel.Lession + " and [WeekDay]=" + arrModel.WeekDay + " and CourseID<>" + arrModel.CourseID + " and ClassID=" + arrModel.ClassID + " and ScoreTerm='" + arrModel.ScoreTerm + "' and Year='" + arrModel.Year + "'");//班级安排冲突
            if (arrModelList4.Count > 0)
            {
                checkArrangement[3] = arrModelList4[0].CourseName;
            }

            List<LabMS.Model.Arrangement> arrModelList5 = new List<LabMS.Model.Arrangement>();
            arrModelList5 = arr.GetModelList(" [Week]='" + arrModel.Week + "' and Lession=" + arrModel.Lession + " and [WeekDay]=" + arrModel.WeekDay + " and LocationID=" + arrModel.LocationID + " and TeacherID<>" + arrModel.TeacherID + " and ScoreTerm='" + arrModel.ScoreTerm + "' and Year='" + arrModel.Year + "'");//老师地点安排冲突(同一个地点2个老师上课)
            if (arrModelList5.Count > 0)
            {
                checkArrangement[4] = arrModelList5[0].TeacherName;
            }

            List<LabMS.Model.Arrangement> arrModelList3 = new List<LabMS.Model.Arrangement>();
            arrModelList3 = arr.GetModelList(" [Week]='" + arrModel.Week + "' and Lession=" + arrModel.Lession + " and [WeekDay]=" + arrModel.WeekDay + " and CourseID=" + arrModel.CourseID + " and TeacherID=" + arrModel.TeacherID + " and ClassID=" + arrModel.ClassID + " and LocationID=" + arrModel.LocationID);//不添加
            if (arrModelList3.Count > 0)
            {
                checkArrangement[2] = arrModelList3[0].ID.ToString();
            }

            return checkArrangement;
        }


        protected void Concel_OnClick(object sender, EventArgs e)
        {
            tblistEmpty2.Value = "1";
            tblistStudents2.Value = "";
            tblistEmpty.Value = "1";
            tblistStudents.Value = "";
            arrtable.Visible = false;
            YearDDL.Enabled = true;
            TermDDL.Enabled = true;
            CourseDDL.Enabled = true;
            DeleteBtn.Enabled = true;
            ArrangementGridView.Enabled = true;

        }


    }
}
